package com.mango.ch11;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

public class CompositeKeySortComparetor extends WritableComparator {
    public CompositeKeySortComparetor() {
        super(CompositeKey.class, true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        CompositeKey c1 = (CompositeKey) a;
        CompositeKey c2 = (CompositeKey) b;
        int com = c1.getName().compareTo(c2.getName());
        if (com == 0) {
            Long secom = c1.getDate() - c2.getDate();
            if (secom != 0) {
                return secom > 0 ? 1 : -1;
            } else
                return 0;
        } else {
            return com;
        }
    }
}
